""" 

    DigitalIndex.py used in RepresentedBy Facebook application
    http://apps.facebook.com/representedby/
        
"""

__author__ = "Will Turnage (will@wubbahed.com)"
__version__ = "0.1.0"
__copyright__ = "Copyright (c) 2009 wubbahed.com"
__license__ = "GPLv3"


import os
import wsgiref.handlers
import facebook
import const
import string

from sets import Set
from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
from google.appengine.ext import db
from utils import fbUtil, dbUtil, UserDistrict, SunlightUtil

class MainHandler(webapp.RequestHandler):
        
    def post(self):
    
        try:
            #check if Facebook app is installed
            self.facebookapi = fbUtil().checkFacebookAppInstalled(self, )
            if self.facebookapi is None:
                return
                   
            user = self.facebookapi.users.getInfo([self.facebookapi.uid], ['uid', 'name', 'current_location'])[0]
            userInfo = dbUtil().getLegislatorForUser(user["uid"], user["current_location"], True)
            if userInfo is None:
                self.response.out.write('<fb:redirect url="/representedby/location" />')
            else:  
                

                legislator_keys = ["rep", "jrsen", "srsen"]          
                chartInfo = {"x":"", "y":"", "moneyy":"", "weight":"", "arrows1":"", "arrows2":"", "stats":[]}
                chartInfo["section"] = "goodies"
                chartInfo["discussion_board"] = {"id": "", "name": "your legislators", "modifier": "with"}
                chartInfo["loggedin"] = True
            
                path = os.path.join(os.path.dirname(__file__), '../templates/heading.fbml')
                self.response.out.write(template.render(path, chartInfo))

                dataset = {"A000014": {"earmarks": 760000, "years": 26, "digitalindex": 75}, "A000022": {"earmarks": 630000, "years": 28, "digitalindex": 20}, "A000055": {"earmarks": 5192000, "years": 14, "digitalindex": 40}, "A000069": {"earmarks": 835000, "years": 36, "digitalindex": 0}, "A000210": {"earmarks": 594250, "years": 22, "digitalindex": 0}, "A000358": {"earmarks": 1070000, "years": 10, "digitalindex": 0}, "A000360": {"earmarks": 5402000, "years": 12, "digitalindex": 20}, "A000361": {"earmarks": 671500, "years": 8, "digitalindex": 0}, "A000362": {"earmarks": 3312014, "years": 4, "digitalindex": 20}, "A000363": {"earmarks": 4287014, "years": 4, "digitalindex": 25}, "B000013": {"earmarks": 1795250, "years": 18, "digitalindex": 0}, "B000208": {"earmarks": 2211350, "years": 18, "digitalindex": 0}, "B000213": {"earmarks": 3247763, "years": 26, "digitalindex": 20}, "B000243": {"earmarks": 2496750, "years": 40, "digitalindex": 0}, "B000287": {"earmarks": 2196215, "years": 18, "digitalindex": 20}, "B000382": {"earmarks": 18026500, "years": 18, "digitalindex": 0}, "B000410": {"earmarks": 2353000, "years": 28, "digitalindex": 0}, "B000420": {"earmarks": 12885643, "years": 14, "digitalindex": 20}, "B000461": {"earmarks": 1497500, "years": 16, "digitalindex": 20}, "B000468": {"earmarks": 13807750, "years": 30, "digitalindex": 25}, "B000490": {"earmarks": 3960144, "years": 18, "digitalindex": 0}, "B000574": {"earmarks": 0, "years": 16, "digitalindex": 50}, "B000575": {"earmarks": 6845250, "years": 14, "digitalindex": 45}, "B000589": {"earmarks": 0, "years": 20, "digitalindex": 75}, "B000611": {"earmarks": 73557491, "years": 24, "digitalindex": 20}, "B000652": {"earmarks": 2030000, "years": 14, "digitalindex": 20}, "B000657": {"earmarks": 2564000, "years": 28, "digitalindex": 0}, "B000711": {"earmarks": 7546250, "years": 28, "digitalindex": 45}, "B000716": {"earmarks": 4231000, "years": 14, "digitalindex": 0}, "B000755": {"earmarks": 6376000, "years": 14, "digitalindex": 45}, "B000911": {"earmarks": 808000, "years": 18, "digitalindex": 0}, "B000944": {"earmarks": 3161500, "years": 20, "digitalindex": 20}, "B000953": {"earmarks": 12020048, "years": 16, "digitalindex": 0}, "B001066": {"earmarks": 735000, "years": 24, "digitalindex": 0}, "B001135": {"earmarks": 1284000, "years": 16, "digitalindex": 20}, "B001149": {"earmarks": 0, "years": 28, "digitalindex": 40}, "B001203": {"earmarks": 906000, "years": 18, "digitalindex": 0}, "B001210": {"earmarks": 122804900, "years": 60, "digitalindex": 0}, "B001227": {"earmarks": 1929500, "years": 14, "digitalindex": 20}, "B001228": {"earmarks": 3132500, "years": 13, "digitalindex": 0}, "B001229": {"earmarks": 988000, "years": 12, "digitalindex": 0}, "B001230": {"earmarks": 1714500, "years": 12, "digitalindex": 0}, "B001231": {"earmarks": 462000, "years": 12, "digitalindex": 0}, "B001232": {"earmarks": 0, "years": 12, "digitalindex": 25}, "B001233": {"earmarks": 1188000, "years": 12, "digitalindex": 0}, "B001234": {"earmarks": 3170414, "years": 12, "digitalindex": 0}, "B001235": {"earmarks": 1623000, "years": 10, "digitalindex": 25}, "B001236": {"earmarks": 237500, "years": 9, "digitalindex": 45}, "B001239": {"earmarks": 570000, "years": 8, "digitalindex": 60}, "B001242": {"earmarks": 2487514, "years": 8, "digitalindex": 20}, "B001243": {"earmarks": 0, "years": 8, "digitalindex": 0}, "B001244": {"earmarks": 23511000, "years": 8, "digitalindex": 0}, "B001247": {"earmarks": 480000, "years": 8, "digitalindex": 0}, "B001248": {"earmarks": 9913304, "years": 8, "digitalindex": 70}, "B001250": {"earmarks": 618000, "years": 8, "digitalindex": 0}, "B001251": {"earmarks": 1599514, "years": 6, "digitalindex": 0}, "B001252": {"earmarks": 2780000, "years": 6, "digitalindex": 0}, "B001253": {"earmarks": 3697314, "years": 6, "digitalindex": 0}, "B001254": {"earmarks": 3086000, "years": 6, "digitalindex": 0}, "B001255": {"earmarks": 0, "years": 6, "digitalindex": 0}, "B001256": {"earmarks": 0, "years": 4, "digitalindex": 70}, "B001257": {"earmarks": 880000, "years": 4, "digitalindex": 20}, "B001259": {"earmarks": 475000, "years": 4, "digitalindex": 0}, "B001260": {"earmarks": 5313875, "years": 4, "digitalindex": 0}, "B001261": {"earmarks": 2713000, "years": 6, "digitalindex": 0}, "B001262": {"earmarks": 0, "years": 3, "digitalindex": 30}, "C000059": {"earmarks": 5684500, "years": 18, "digitalindex": 0}, "C000071": {"earmarks": 0, "years": 20, "digitalindex": 0}, "C000127": {"earmarks": 143000, "years": 20, "digitalindex": 20}, "C000141": {"earmarks": 1271000, "years": 26, "digitalindex": 20}, "C000174": {"earmarks": 0, "years": 30, "digitalindex": 0}, "C000243": {"earmarks": 688750, "years": 18, "digitalindex": 20}, "C000286": {"earmarks": 4253000, "years": 20, "digitalindex": 25}, "C000537": {"earmarks": 25585022, "years": 18, "digitalindex": 0}, "C000556": {"earmarks": 3295500, "years": 26, "digitalindex": 45}, "C000560": {"earmarks": 0, "years": 16, "digitalindex": 25}, "C000567": {"earmarks": 75908475, "years": 42, "digitalindex": 0}, "C000705": {"earmarks": 0, "years": 26, "digitalindex": 0}, "C000714": {"earmarks": 1522800, "years": 46, "digitalindex": 25}, "C000754": {"earmarks": 0, "years": 28, "digitalindex": 20}, "C000794": {"earmarks": 5425175, "years": 24, "digitalindex": 0}, "C000880": {"earmarks": 100000, "years": 18, "digitalindex": 0}, "C000984": {"earmarks": 675000, "years": 16, "digitalindex": 20}, "C001035": {"earmarks": 380000, "years": 18, "digitalindex": 40}, "C001036": {"earmarks": 13069000, "years": 13, "digitalindex": 0}, "C001037": {"earmarks": 4196000, "years": 12, "digitalindex": 0}, "C001038": {"earmarks": 2315514, "years": 12, "digitalindex": 25}, "C001045": {"earmarks": 2525000, "years": 10, "digitalindex": 0}, "C001046": {"earmarks": 0, "years": 10, "digitalindex": 45}, "C001047": {"earmarks": 5075000, "years": 10, "digitalindex": 20}, "C001048": {"earmarks": 6592500, "years": 10, "digitalindex": 30}, "C001049": {"earmarks": 1975500, "years": 10, "digitalindex": 20}, "C001050": {"earmarks": 1472500, "years": 8, "digitalindex": 0}, "C001051": {"earmarks": 5980000, "years": 8, "digitalindex": 40}, "C001053": {"earmarks": 2426125, "years": 8, "digitalindex": 0}, "C001056": {"earmarks": 1142000, "years": 13, "digitalindex": 45}, "C001058": {"earmarks": 4737643, "years": 7, "digitalindex": 0}, "C001059": {"earmarks": 1655000, "years": 6, "digitalindex": 0}, "C001060": {"earmarks": 1950500, "years": 6, "digitalindex": 25}, "C001061": {"earmarks": 2735500, "years": 6, "digitalindex": 0}, "C001062": {"earmarks": 5987000, "years": 6, "digitalindex": 20}, "C001063": {"earmarks": 2897764, "years": 6, "digitalindex": 0}, "C001064": {"earmarks": 0, "years": 5, "digitalindex": 20}, "C001065": {"earmarks": 1410000, "years": 4, "digitalindex": 0}, "C001066": {"earmarks": 1794500, "years": 4, "digitalindex": 0}, "C001067": {"earmarks": 431257, "years": 4, "digitalindex": 20}, "C001068": {"earmarks": 1967000, "years": 4, "digitalindex": 50}, "C001069": {"earmarks": 2014500, "years": 4, "digitalindex": 45}, "C001070": {"earmarks": 27169750, "years": 6, "digitalindex": 0}, "C001071": {"earmarks": 760000, "years": 6, "digitalindex": 20}, "C001072": {"earmarks": 1152014, "years": 3, "digitalindex": 0}, "C001074": {"earmarks": 1368000, "years": 3, "digitalindex": 0}, "D000096": {"earmarks": 3066014, "years": 14, "digitalindex": 0}, "D000168": {"earmarks": 0, "years": 18, "digitalindex": 0}, "D000191": {"earmarks": 7908000, "years": 24, "digitalindex": 20}, "D000197": {"earmarks": 0, "years": 14, "digitalindex": 40}, "D000210": {"earmarks": 11679000, "years": 14, "digitalindex": 45}, "D000216": {"earmarks": 7109500, "years": 20, "digitalindex": 0}, "D000299": {"earmarks": 4984900, "years": 18, "digitalindex": 0}, "D000327": {"earmarks": 9214272, "years": 34, "digitalindex": 20}, "D000355": {"earmarks": 3444000, "years": 56, "digitalindex": 25}, "D000388": {"earmarks": 0, "years": 36, "digitalindex": 60}, "D000399": {"earmarks": 1261500, "years": 16, "digitalindex": 20}, "D000432": {"earmarks": 36547100, "years": 30, "digitalindex": 45}, "D000482": {"earmarks": 5471000, "years": 16, "digitalindex": 65}, "D000492": {"earmarks": 5152500, "years": 30, "digitalindex": 0}, "D000533": {"earmarks": 2045000, "years": 24, "digitalindex": 20}, "D000563": {"earmarks": 31335250, "years": 32, "digitalindex": 20}, "D000595": {"earmarks": 0, "years": 12, "digitalindex": 65}, "D000598": {"earmarks": 1343000, "years": 10, "digitalindex": 0}, "D000599": {"earmarks": 1284000, "years": 8, "digitalindex": 0}, "D000600": {"earmarks": 2637250, "years": 8, "digitalindex": 40}, "D000602": {"earmarks": 1502014, "years": 8, "digitalindex": 0}, "D000603": {"earmarks": 2555950, "years": 6, "digitalindex": 20}, "D000604": {"earmarks": 5019604, "years": 6, "digitalindex": 0}, "D000607": {"earmarks": 2336000, "years": 4, "digitalindex": 25}, "E000063": {"earmarks": 24797271, "years": 20, "digitalindex": 0}, "E000092": {"earmarks": 1249160, "years": 18, "digitalindex": 20}, "E000172": {"earmarks": 17074000, "years": 14, "digitalindex": 0}, "E000179": {"earmarks": 1564514, "years": 22, "digitalindex": 0}, "E000194": {"earmarks": 0, "years": 18, "digitalindex": 55}, "E000215": {"earmarks": 1350000, "years": 18, "digitalindex": 20}, "E000226": {"earmarks": 1415775, "years": 14, "digitalindex": 0}, "E000285": {"earmarks": 1725000, "years": 18, "digitalindex": 20}, "E000288": {"earmarks": 903000, "years": 4, "digitalindex": 65}, "E000289": {"earmarks": 3197500, "years": 4, "digitalindex": 0}, "E000290": {"earmarks": 0, "years": 3, "digitalindex": 0}, "F000030": {"earmarks": 8293000, "years": 18, "digitalindex": 0}, "F000043": {"earmarks": 12232643, "years": 16, "digitalindex": 0}, "F000061": {"earmarks": 0, "years": 18, "digitalindex": 40}, "F000062": {"earmarks": 56444425, "years": 21, "digitalindex": 20}, "F000116": {"earmarks": 1292000, "years": 18, "digitalindex": 0}, "F000339": {"earmarks": 1425000, "years": 30, "digitalindex": 0}, "F000372": {"earmarks": 6265750, "years": 16, "digitalindex": 0}, "F000444": {"earmarks": 0, "years": 10, "digitalindex": 40}, "F000445": {"earmarks": 3862500, "years": 10, "digitalindex": 20}, "F000448": {"earmarks": 0, "years": 8, "digitalindex": 50}, "F000449": {"earmarks": 1045000, "years": 6, "digitalindex": 25}, "F000450": {"earmarks": 0, "years": 6, "digitalindex": 50}, "F000453": {"earmarks": 1050000, "years": 4, "digitalindex": 25}, "F000454": {"earmarks": 3096000, "years": 3, "digitalindex": 0}, "G000021": {"earmarks": 5265250, "years": 24, "digitalindex": 0}, "G000289": {"earmarks": 160000, "years": 18, "digitalindex": 0}, "G000309": {"earmarks": 4215750, "years": 26, "digitalindex": 20}, "G000359": {"earmarks": 9545000, "years": 20, "digitalindex": 0}, "G000377": {"earmarks": 1889000, "years": 14, "digitalindex": 0}, "G000386": {"earmarks": 355000, "years": 36, "digitalindex": 45}, "G000410": {"earmarks": 500000, "years": 18, "digitalindex": 0}, "G000445": {"earmarks": 10028000, "years": 30, "digitalindex": 0}, "G000535": {"earmarks": 760000, "years": 18, "digitalindex": 20}, "G000544": {"earmarks": 1114514, "years": 12, "digitalindex": 0}, "G000546": {"earmarks": 4930500, "years": 10, "digitalindex": 20}, "G000548": {"earmarks": 1392500, "years": 8, "digitalindex": 0}, "G000549": {"earmarks": 3875000, "years": 8, "digitalindex": 0}, "G000550": {"earmarks": 31540250, "years": 8, "digitalindex": 25}, "G000551": {"earmarks": 2400000, "years": 8, "digitalindex": 0}, "G000552": {"earmarks": 0, "years": 6, "digitalindex": 20}, "G000553": {"earmarks": 970000, "years": 6, "digitalindex": 0}, "G000554": {"earmarks": 1729765, "years": 4, "digitalindex": 20}, "G000555": {"earmarks": 2999000, "years": 4, "digitalindex": 0}, "H000067": {"earmarks": 2449100, "years": 30, "digitalindex": 0}, "H000206": {"earmarks": 66860000, "years": 40, "digitalindex": 20}, "H000213": {"earmarks": 2408000, "years": 18, "digitalindex": 0}, "H000324": {"earmarks": 2407000, "years": 18, "digitalindex": 0}, "H000329": {"earmarks": 1736000, "years": 16, "digitalindex": 20}, "H000338": {"earmarks": 711000, "years": 36, "digitalindex": 0}, "H000528": {"earmarks": 4998450, "years": 24, "digitalindex": 40}, "H000627": {"earmarks": 4890036, "years": 18, "digitalindex": 20}, "H000636": {"earmarks": 2607938, "years": 14, "digitalindex": 20}, "H000676": {"earmarks": 2928250, "years": 18, "digitalindex": 25}, "H000712": {"earmarks": 1477572, "years": 18, "digitalindex": 0}, "H000874": {"earmarks": 7977757, "years": 30, "digitalindex": 20}, "H001016": {"earmarks": 9851000, "years": 20, "digitalindex": 25}, "H001030": {"earmarks": 3009000, "years": 12, "digitalindex": 20}, "H001032": {"earmarks": 652500, "years": 12, "digitalindex": 45}, "H001034": {"earmarks": 3665643, "years": 10, "digitalindex": 65}, "H001036": {"earmarks": 0, "years": 8, "digitalindex": 20}, "H001037": {"earmarks": 422000, "years": 7, "digitalindex": 45}, "H001038": {"earmarks": 2265514, "years": 6, "digitalindex": 0}, "H001039": {"earmarks": 2117748, "years": 4, "digitalindex": 0}, "H001040": {"earmarks": 2031000, "years": 4, "digitalindex": 0}, "H001041": {"earmarks": 1928500, "years": 4, "digitalindex": 25}, "H001042": {"earmarks": 1499000, "years": 4, "digitalindex": 20}, "H001043": {"earmarks": 2670237, "years": 4, "digitalindex": 0}, "H001048": {"earmarks": 2564500, "years": 2, "digitalindex": 20}, "I000023": {"earmarks": 1205000, "years": 18, "digitalindex": 45}, "I000024": {"earmarks": 47826500, "years": 28, "digitalindex": 40}, "I000025": {"earmarks": 45843205, "years": 52, "digitalindex": 0}, "I000026": {"earmarks": 475750, "years": 18, "digitalindex": 20}, "I000055": {"earmarks": 1425000, "years": 12, "digitalindex": 0}, "I000056": {"earmarks": 2142000, "years": 10, "digitalindex": 0}, "I000057": {"earmarks": 2716639, "years": 10, "digitalindex": 20}, "J000032": {"earmarks": 908000, "years": 16, "digitalindex": 45}, "J000126": {"earmarks": 3019000, "years": 18, "digitalindex": 0}, "J000174": {"earmarks": 571000, "years": 20, "digitalindex": 0}, "J000177": {"earmarks": 12341000, "years": 28, "digitalindex": 0}, "J000255": {"earmarks": 0, "years": 16, "digitalindex": 0}, "J000283": {"earmarks": 3029142, "years": 16, "digitalindex": 20}, "J000285": {"earmarks": 2806100, "years": 10, "digitalindex": 0}, "J000288": {"earmarks": 1094750, "years": 4, "digitalindex": 20}, "J000289": {"earmarks": 0, "years": 4, "digitalindex": 65}, "K000008": {"earmarks": 3119000, "years": 26, "digitalindex": 0}, "K000009": {"earmarks": 13108321, "years": 28, "digitalindex": 0}, "K000105": {"earmarks": 714000, "years": 51, "digitalindex": 0}, "K000113": {"earmarks": 3925500, "years": 16, "digitalindex": 25}, "K000148": {"earmarks": 0, "years": 30, "digitalindex": 0}, "K000172": {"earmarks": 1138000, "years": 34, "digitalindex": 0}, "K000180": {"earmarks": 1934000, "years": 14, "digitalindex": 0}, "K000188": {"earmarks": 0, "years": 14, "digitalindex": 20}, "K000210": {"earmarks": 655500, "years": 18, "digitalindex": 25}, "K000220": {"earmarks": 666000, "years": 18, "digitalindex": 75}, "K000305": {"earmarks": 23832000, "years": 24, "digitalindex": 0}, "K000336": {"earmarks": 863000, "years": 14, "digitalindex": 40}, "K000352": {"earmarks": 4950000, "years": 26, "digitalindex": 20}, "K000360": {"earmarks": 0, "years": 10, "digitalindex": 0}, "K000362": {"earmarks": 807500, "years": 8, "digitalindex": 25}, "K000363": {"earmarks": 0, "years": 8, "digitalindex": 20}, "K000365": {"earmarks": 2365500, "years": 4, "digitalindex": 20}, "K000366": {"earmarks": 2182000, "years": 4, "digitalindex": 45}, "K000367": {"earmarks": 4740000, "years": 6, "digitalindex": 0}, "L000035": {"earmarks": 0, "years": 18, "digitalindex": 20}, "L000111": {"earmarks": 3720750, "years": 16, "digitalindex": 75}, "L000123": {"earmarks": 760450, "years": 33, "digitalindex": 20}, "L000174": {"earmarks": 36161125, "years": 36, "digitalindex": 45}, "L000261": {"earmarks": 3800000, "years": 36, "digitalindex": 0}, "L000263": {"earmarks": 1426750, "years": 28, "digitalindex": 0}, "L000274": {"earmarks": 16290000, "years": 32, "digitalindex": 0}, "L000287": {"earmarks": 365500, "years": 24, "digitalindex": 20}, "L000304": {"earmarks": 475000, "years": 24, "digitalindex": 0}, "L000321": {"earmarks": 0, "years": 18, "digitalindex": 0}, "L000397": {"earmarks": 902500, "years": 16, "digitalindex": 20}, "L000480": {"earmarks": 4411617, "years": 22, "digitalindex": 0}, "L000491": {"earmarks": 975450, "years": 18, "digitalindex": 20}, "L000504": {"earmarks": 3276000, "years": 36, "digitalindex": 25}, "L000517": {"earmarks": 1335500, "years": 32, "digitalindex": 45}, "L000550": {"earmarks": 10328500, "years": 18, "digitalindex": 0}, "L000551": {"earmarks": 11866143, "years": 14, "digitalindex": 20}, "L000553": {"earmarks": 2153000, "years": 16, "digitalindex": 0}, "L000554": {"earmarks": 1245000, "years": 16, "digitalindex": 0}, "L000557": {"earmarks": 2110000, "years": 12, "digitalindex": 0}, "L000560": {"earmarks": 155000, "years": 10, "digitalindex": 0}, "L000562": {"earmarks": 3775000, "years": 9, "digitalindex": 50}, "L000563": {"earmarks": 4451072, "years": 6, "digitalindex": 0}, "L000564": {"earmarks": 0, "years": 4, "digitalindex": 25}, "L000565": {"earmarks": 1756000, "years": 4, "digitalindex": 0}, "L000566": {"earmarks": 190000, "years": 3, "digitalindex": 70}, "M000087": {"earmarks": 1445000, "years": 18, "digitalindex": 0}, "M000133": {"earmarks": 2696750, "years": 36, "digitalindex": 55}, "M000303": {"earmarks": 0, "years": 28, "digitalindex": 30}, "M000309": {"earmarks": 2181764, "years": 14, "digitalindex": 0}, "M000312": {"earmarks": 2470072, "years": 14, "digitalindex": 0}, "M000355": {"earmarks": 51186000, "years": 30, "digitalindex": 20}, "M000404": {"earmarks": 375000, "years": 22, "digitalindex": 0}, "M000472": {"earmarks": 1481475, "years": 18, "digitalindex": 20}, "M000485": {"earmarks": 1113000, "years": 14, "digitalindex": 20}, "M000508": {"earmarks": 4174500, "years": 18, "digitalindex": 45}, "M000639": {"earmarks": 0, "years": 20, "digitalindex": 45}, "M000689": {"earmarks": 5295000, "years": 18, "digitalindex": 0}, "M000725": {"earmarks": 2970000, "years": 36, "digitalindex": 85}, "M000844": {"earmarks": 10831000, "years": 28, "digitalindex": 25}, "M000933": {"earmarks": 6717664, "years": 20, "digitalindex": 20}, "M000934": {"earmarks": 1170500, "years": 14, "digitalindex": 0}, "M001111": {"earmarks": 39228250, "years": 18, "digitalindex": 0}, "M001120": {"earmarks": 20134772, "years": 38, "digitalindex": 20}, "M001134": {"earmarks": 760000, "years": 16, "digitalindex": 0}, "M001137": {"earmarks": 1228014, "years": 14, "digitalindex": 20}, "M001138": {"earmarks": 2070500, "years": 18, "digitalindex": 45}, "M001139": {"earmarks": 1855500, "years": 12, "digitalindex": 25}, "M001140": {"earmarks": 305000, "years": 12, "digitalindex": 40}, "M001142": {"earmarks": 2847000, "years": 10, "digitalindex": 0}, "M001143": {"earmarks": 950000, "years": 10, "digitalindex": 20}, "M001144": {"earmarks": 691000, "years": 9, "digitalindex": 0}, "M001146": {"earmarks": 2444500, "years": 8, "digitalindex": 20}, "M001147": {"earmarks": 0, "years": 8, "digitalindex": 45}, "M001148": {"earmarks": 2531514, "years": 8, "digitalindex": 60}, "M001149": {"earmarks": 1784000, "years": 8, "digitalindex": 25}, "M001150": {"earmarks": 2495800, "years": 8, "digitalindex": 40}, "M001151": {"earmarks": 2490500, "years": 8, "digitalindex": 0}, "M001153": {"earmarks": 73276750, "years": 8, "digitalindex": 20}, "M001154": {"earmarks": 380000, "years": 8, "digitalindex": 25}, "M001155": {"earmarks": 2542500, "years": 6, "digitalindex": 0}, "M001156": {"earmarks": 0, "years": 6, "digitalindex": 0}, "M001157": {"earmarks": 0, "years": 6, "digitalindex": 40}, "M001158": {"earmarks": 0, "years": 6, "digitalindex": 20}, "M001159": {"earmarks": 0, "years": 6, "digitalindex": 65}, "M001160": {"earmarks": 1878250, "years": 6, "digitalindex": 50}, "M001161": {"earmarks": 0, "years": 6, "digitalindex": 0}, "M001162": {"earmarks": 12320000, "years": 6, "digitalindex": 40}, "M001163": {"earmarks": 928000, "years": 6, "digitalindex": 0}, "M001165": {"earmarks": 0, "years": 4, "digitalindex": 75}, "M001166": {"earmarks": 2510514, "years": 4, "digitalindex": 25}, "M001167": {"earmarks": 2570000, "years": 4, "digitalindex": 0}, "M001168": {"earmarks": 4660014, "years": 4, "digitalindex": 0}, "M001169": {"earmarks": 2740000, "years": 4, "digitalindex": 25}, "M001170": {"earmarks": 0, "years": 6, "digitalindex": 30}, "M001176": {"earmarks": 8229625, "years": 6, "digitalindex": 20}, "N000002": {"earmarks": 3061500, "years": 20, "digitalindex": 0}, "N000015": {"earmarks": 1425000, "years": 22, "digitalindex": 0}, "N000032": {"earmarks": 5715750, "years": 34, "digitalindex": 20}, "N000179": {"earmarks": 2637000, "years": 12, "digitalindex": 0}, "N000180": {"earmarks": 5506000, "years": 12, "digitalindex": 20}, "N000182": {"earmarks": 2614500, "years": 8, "digitalindex": 0}, "O000006": {"earmarks": 5559643, "years": 36, "digitalindex": 0}, "O000007": {"earmarks": 33782000, "years": 42, "digitalindex": 20}, "O000085": {"earmarks": 19450047, "years": 20, "digitalindex": 0}, "O000107": {"earmarks": 1952514, "years": 28, "digitalindex": 0}, "P000034": {"earmarks": 1413000, "years": 24, "digitalindex": 25}, "P000096": {"earmarks": 395000, "years": 14, "digitalindex": 0}, "P000099": {"earmarks": 10225860, "years": 20, "digitalindex": 0}, "P000149": {"earmarks": 862500, "years": 22, "digitalindex": 0}, "P000197": {"earmarks": 15667000, "years": 24, "digitalindex": 80}, "P000258": {"earmarks": 458000, "years": 20, "digitalindex": 0}, "P000265": {"earmarks": 1007300, "years": 32, "digitalindex": 25}, "P000422": {"earmarks": 1800000, "years": 18, "digitalindex": 20}, "P000523": {"earmarks": 7013500, "years": 24, "digitalindex": 20}, "P000583": {"earmarks": 14877750, "years": 36, "digitalindex": 40}, "P000586": {"earmarks": 3568625, "years": 10, "digitalindex": 20}, "P000590": {"earmarks": 0, "years": 12, "digitalindex": 20}, "P000592": {"earmarks": 2634500, "years": 6, "digitalindex": 0}, "P000593": {"earmarks": 2443125, "years": 4, "digitalindex": 20}, "R000004": {"earmarks": 1246000, "years": 16, "digitalindex": 45}, "R000011": {"earmarks": 5608500, "years": 34, "digitalindex": 0}, "R000053": {"earmarks": 2296075, "years": 40, "digitalindex": 0}, "R000122": {"earmarks": 10755750, "years": 24, "digitalindex": 0}, "R000146": {"earmarks": 26628613, "years": 28, "digitalindex": 25}, "R000170": {"earmarks": 4164839, "years": 14, "digitalindex": 0}, "R000307": {"earmarks": 2202000, "years": 34, "digitalindex": 0}, "R000361": {"earmarks": 0, "years": 30, "digitalindex": 0}, "R000395": {"earmarks": 23813500, "years": 30, "digitalindex": 0}, "R000409": {"earmarks": 2020500, "years": 22, "digitalindex": 50}, "R000435": {"earmarks": 1806125, "years": 22, "digitalindex": 45}, "R000462": {"earmarks": 2045000, "years": 14, "digitalindex": 0}, "R000486": {"earmarks": 6415643, "years": 18, "digitalindex": 20}, "R000487": {"earmarks": 788000, "years": 18, "digitalindex": 20}, "R000515": {"earmarks": 2073375, "years": 18, "digitalindex": 20}, "R000568": {"earmarks": 4330643, "years": 14, "digitalindex": 0}, "R000571": {"earmarks": 1642000, "years": 10, "digitalindex": 45}, "R000572": {"earmarks": 2855500, "years": 10, "digitalindex": 0}, "R000573": {"earmarks": 1445500, "years": 10, "digitalindex": 20}, "R000575": {"earmarks": 1216250, "years": 8, "digitalindex": 40}, "R000576": {"earmarks": 3865286, "years": 8, "digitalindex": 0}, "R000577": {"earmarks": 9244000, "years": 8, "digitalindex": 40}, "R000580": {"earmarks": 2655330, "years": 4, "digitalindex": 20}, "R000581": {"earmarks": 2326514, "years": 3, "digitalindex": 45}, "S000030": {"earmarks": 1860500, "years": 14, "digitalindex": 0}, "S000033": {"earmarks": 5877725, "years": 22, "digitalindex": 0}, "S000148": {"earmarks": 16424250, "years": 30, "digitalindex": 20}, "S000185": {"earmarks": 4113500, "years": 18, "digitalindex": 50}, "S000248": {"earmarks": 7042043, "years": 22, "digitalindex": 25}, "S000250": {"earmarks": 1712500, "years": 14, "digitalindex": 0}, "S000320": {"earmarks": 114484250, "years": 32, "digitalindex": 25}, "S000344": {"earmarks": 6538000, "years": 14, "digitalindex": 0}, "S000364": {"earmarks": 2421750, "years": 14, "digitalindex": 25}, "S000465": {"earmarks": 3111643, "years": 34, "digitalindex": 0}, "S000480": {"earmarks": 1827507, "years": 24, "digitalindex": 20}, "S000510": {"earmarks": 1250000, "years": 14, "digitalindex": 0}, "S000522": {"earmarks": 1317375, "years": 30, "digitalindex": 20}, "S000583": {"earmarks": 4742500, "years": 24, "digitalindex": 0}, "S000663": {"earmarks": 0, "years": 34, "digitalindex": 0}, "S000672": {"earmarks": 16831500, "years": 14, "digitalindex": 45}, "S000709": {"earmarks": 25320000, "years": 30, "digitalindex": 0}, "S000749": {"earmarks": 2793600, "years": 28, "digitalindex": 20}, "S000770": {"earmarks": 0, "years": 16, "digitalindex": 40}, "S000810": {"earmarks": 2441000, "years": 38, "digitalindex": 40}, "S000822": {"earmarks": 963000, "years": 22, "digitalindex": 0}, "S001045": {"earmarks": 2374500, "years": 18, "digitalindex": 20}, "S001141": {"earmarks": 4250500, "years": 18, "digitalindex": 0}, "S001143": {"earmarks": 717500, "years": 16, "digitalindex": 25}, "S001145": {"earmarks": 1644000, "years": 12, "digitalindex": 45}, "S001148": {"earmarks": 3193250, "years": 12, "digitalindex": 0}, "S001150": {"earmarks": 3632286, "years": 10, "digitalindex": 0}, "S001154": {"earmarks": 5220500, "years": 10, "digitalindex": 25}, "S001155": {"earmarks": 1852500, "years": 9, "digitalindex": 20}, "S001156": {"earmarks": 3112350, "years": 8, "digitalindex": 0}, "S001157": {"earmarks": 1520500, "years": 8, "digitalindex": 0}, "S001158": {"earmarks": 95000, "years": 6, "digitalindex": 0}, "S001162": {"earmarks": 2252500, "years": 6, "digitalindex": 20}, "S001164": {"earmarks": 1417500, "years": 5, "digitalindex": 0}, "S001165": {"earmarks": 285000, "years": 4, "digitalindex": 20}, "S001168": {"earmarks": 0, "years": 4, "digitalindex": 0}, "S001169": {"earmarks": 2682000, "years": 4, "digitalindex": 25}, "S001170": {"earmarks": 1624250, "years": 4, "digitalindex": 0}, "S001171": {"earmarks": 4376264, "years": 4, "digitalindex": 0}, "S001172": {"earmarks": 475000, "years": 4, "digitalindex": 25}, "S001173": {"earmarks": 2115000, "years": 4, "digitalindex": 0}, "S001174": {"earmarks": 4634000, "years": 4, "digitalindex": 0}, "S001175": {"earmarks": 855500, "years": 3, "digitalindex": 0}, "S001176": {"earmarks": 237500, "years": 3, "digitalindex": 50}, "T000038": {"earmarks": 1931500, "years": 22, "digitalindex": 0}, "T000057": {"earmarks": 4969000, "years": 14, "digitalindex": 20}, "T000074": {"earmarks": 1508500, "years": 22, "digitalindex": 0}, "T000193": {"earmarks": 3209143, "years": 18, "digitalindex": 0}, "T000250": {"earmarks": 4275000, "years": 14, "digitalindex": 20}, "T000260": {"earmarks": 3510000, "years": 16, "digitalindex": 20}, "T000266": {"earmarks": 1292675, "years": 14, "digitalindex": 0}, "T000326": {"earmarks": 993257, "years": 28, "digitalindex": 20}, "T000459": {"earmarks": 0, "years": 12, "digitalindex": 25}, "T000460": {"earmarks": 6541000, "years": 12, "digitalindex": 20}, "T000463": {"earmarks": 7017250, "years": 8, "digitalindex": 0}, "T000464": {"earmarks": 1863000, "years": 6, "digitalindex": 20}, "T000465": {"earmarks": 2454125, "years": 3, "digitalindex": 0}, "U000031": {"earmarks": 1416475, "years": 24, "digitalindex": 0}, "V000081": {"earmarks": 2102642, "years": 18, "digitalindex": 20}, "V000108": {"earmarks": 34185071, "years": 26, "digitalindex": 0}, "V000126": {"earmarks": 8541000, "years": 12, "digitalindex": 0}, "V000127": {"earmarks": 4034000, "years": 12, "digitalindex": 0}, "V000128": {"earmarks": 1331072, "years": 8, "digitalindex": 0}, "W000119": {"earmarks": 6099000, "years": 16, "digitalindex": 20}, "W000187": {"earmarks": 3186757, "years": 20, "digitalindex": 20}, "W000207": {"earmarks": 1488000, "years": 18, "digitalindex": 0}, "W000314": {"earmarks": 2329000, "years": 14, "digitalindex": 20}, "W000413": {"earmarks": 1569250, "years": 16, "digitalindex": 0}, "W000437": {"earmarks": 4324000, "years": 18, "digitalindex": 20}, "W000672": {"earmarks": 0, "years": 30, "digitalindex": 20}, "W000738": {"earmarks": 3412000, "years": 18, "digitalindex": 0}, "W000779": {"earmarks": 427750, "years": 30, "digitalindex": 20}, "W000791": {"earmarks": 920000, "years": 12, "digitalindex": 50}, "W000792": {"earmarks": 775500, "years": 12, "digitalindex": 45}, "W000793": {"earmarks": 712500, "years": 12, "digitalindex": 20}, "W000794": {"earmarks": 3688500, "years": 10, "digitalindex": 0}, "W000797": {"earmarks": 5223464, "years": 6, "digitalindex": 25}, "W000799": {"earmarks": 475000, "years": 4, "digitalindex": 0}, "W000800": {"earmarks": 1349430, "years": 4, "digitalindex": 0}, "W000801": {"earmarks": 1456000, "years": 4, "digitalindex": 25}, "W000802": {"earmarks": 0, "years": 6, "digitalindex": 20}, "W000803": {"earmarks": 8568000, "years": 6, "digitalindex": 20}, "W000804": {"earmarks": 2778000, "years": 3, "digitalindex": 55}, "W000805": {"earmarks": 95000, "years": 6, "digitalindex": 25}, "Y000031": {"earmarks": 8774250, "years": 40, "digitalindex": 25}, "Y000033": {"earmarks": 22552125, "years": 38, "digitalindex": 20}, "Y000062": {"earmarks": 3859539, "years": 4, "digitalindex": 20} }
        
        
        
        
                chart1Index = len ([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,12,12,13,13,13,13,13,14,14,14,15,15,15,15,15,16,17])
                chart2Index = len ([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,6,6,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,11,11,12,12,13,13,13,14,14,15,15,16,17])
                
                for title in legislator_keys:
                    if userInfo.has_key(title):
                        if userInfo[title] is not None:
                            theID = userInfo[title]
                            if dataset.has_key(theID):
                                indexVal = dataset[theID]["digitalindex"] / 5
                                yearVal = int( dataset[theID]["years"] / 2 )
                                moneyVal = dataset[theID]["earmarks"] / 1000000
                                chartInfo["x"] = chartInfo["x"] + "," + str( indexVal )
                                chartInfo["y"] = chartInfo["y"] + "," + str( yearVal )
                                chartInfo["moneyy"] = chartInfo["moneyy"] + "," + str( moneyVal )
                                chartInfo["weight"] = chartInfo["weight"] + ",12" 
                                chartInfo["arrows1"] = chartInfo["arrows1"] + "|x,99FF99,0," + str( chart1Index ) + ",10"
                                chart1Index = chart1Index + 1
                                chartInfo["arrows2"] = chartInfo["arrows2"] + "|x,99FF99,0," + str( chart2Index ) + ",10"
                                chart2Index = chart2Index + 1
                                if dataset[theID]["digitalindex"] == 0:
                                    dataset[theID]["rating"] = "<h1 style='color:#AA0000'>None</h1>"
                                elif dataset[theID]["digitalindex"] < 40:
                                    dataset[theID]["rating"] = "<h1 style='color:#AAAA00'>Low</h1>"
                                else:
                                    dataset[theID]["rating"] = "<h1 style='color:#339933'>Good</h1>"
                                dataset[theID]["name"] = const.FRIENDLY_NAME[theID]
                                chartInfo["stats"].append(dataset[theID])
           
                path = os.path.join(os.path.dirname(__file__), '../templates/digitalindex.fbml')
                self.response.out.write(template.render(path, chartInfo ))
                  
                
        except urlfetch.DownloadError, e:
            path = os.path.join(os.path.dirname(__file__), '../templates/error.fbml')
            self.response.out.write(template.render(path, {"error":"Sorry, Google AppEngine ain't working"}))
            
        except facebook.FacebookError, e:
            path = os.path.join(os.path.dirname(__file__), '../templates/error.fbml')
            self.response.out.write(template.render(path, {"error":"Sorry, Facebook ain't working - " + str(e.msg)})) 
            
            
        
        
            
  
def main():
	application = webapp.WSGIApplication([('/fb/digitalindex', MainHandler)],
									   debug=True)
	run_wsgi_app(application)

# load our custom filters		   
webapp.template.register_template_library('customfilters')

if __name__ == '__main__':
	main()
